<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userName"style="width: 200px;">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="userId"style="width: 200px;">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考勤月份" prop="khMonth"style="width: 180px;">
        <el-select v-model="queryParams.khMonth" placeholder="请选择考勤月份" clearable>
          <el-option v-for="item in date_list" :key="item" :label="item" :value="item" />
          <!-- <el-option label="请选择字典生成" value="" /> -->
        </el-select>
      </el-form-item>
      <el-form-item label="考勤时段" prop="amPm" style="width: 180px;">
        <el-select v-model="queryParams.amPm" placeholder="请选择考勤时段" clearable>
          <el-option label="上午" value="上午" />
          <el-option label="下午" value="下午" />
        </el-select>
      </el-form-item>
      <el-form-item label="考勤类型" prop="khWork" style="width: 180px;">
        <el-select v-model="queryParams.khWork" placeholder="请选择考勤类型" clearable>
          <el-option label="上班" value="上班" />
          <el-option label="下班" value="下班" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['kaoqin:kh_monthly_attendance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['kaoqin:kh_monthly_attendance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kaoqin:kh_monthly_attendance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['kaoqin:kh_monthly_attendance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kh_monthly_attendanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="考勤月份" align="center" prop="khMonth" />
      <el-table-column label="考勤时段" align="center" prop="amPm" />
      <el-table-column label="考勤类型" align="center" prop="khWork" />
      <el-table-column label="1日" align="center" prop="day1" />
      <el-table-column label="2日" align="center" prop="day2" />
      <el-table-column label="3日" align="center" prop="day3" />
      <el-table-column label="4日" align="center" prop="day4" />
      <el-table-column label="5日" align="center" prop="day5" />
      <el-table-column label="6日" align="center" prop="day6" />
      <el-table-column label="7日" align="center" prop="day7" />
      <el-table-column label="8日" align="center" prop="day8" />
      <el-table-column label="9日" align="center" prop="day9" />
      <el-table-column label="10日" align="center" prop="day10" />
      <el-table-column label="11日" align="center" prop="day11" />
      <el-table-column label="12日" align="center" prop="day12" />
      <el-table-column label="13日" align="center" prop="day13" />
      <el-table-column label="14日" align="center" prop="day14" />
      <el-table-column label="15日" align="center" prop="day15" />
      <el-table-column label="16日" align="center" prop="day16" />
      <el-table-column label="17日" align="center" prop="day17" />
      <el-table-column label="18日" align="center" prop="day18" />
      <el-table-column label="19日" align="center" prop="day19" />
      <el-table-column label="20日" align="center" prop="day20" />
      <el-table-column label="21日" align="center" prop="day21" />
      <el-table-column label="22日" align="center" prop="day22" />
      <el-table-column label="23日" align="center" prop="day23" />
      <el-table-column label="24日" align="center" prop="day24" />
      <el-table-column label="25日" align="center" prop="day25" />
      <el-table-column label="26日" align="center" prop="day26" />
      <el-table-column label="27日" align="center" prop="day27" />
      <el-table-column label="28日" align="center" prop="day28" />
      <el-table-column label="29日" align="center" prop="day29" />
      <el-table-column label="30日" align="center" prop="day30" />
      <el-table-column label="31日" align="center" prop="day31" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['kaoqin:kh_monthly_attendance:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['kaoqin:kh_monthly_attendance:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改考勤月报表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="kh_monthly_attendanceRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="用户姓名" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入用户姓名" />
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入用户编号" />
      </el-form-item>
      <el-form-item label="考勤月份" prop="khMonth">
        <el-select v-model="form.khMonth" placeholder="请选择考勤月份">
          <el-option label="请选择字典生成" value="" />

        </el-select>
      </el-form-item>
      <el-form-item label="考勤时段" prop="amPm">
        <el-select v-model="form.amPm" placeholder="请选择考勤时段">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="考勤类型" prop="khWork">
        <el-select v-model="form.khWork" placeholder="请选择考勤类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="1日" prop="day1">
        <el-input v-model="form.day1" placeholder="请输入1日" />
      </el-form-item>
      <el-form-item label="2日" prop="day2">
        <el-input v-model="form.day2" placeholder="请输入2日" />
      </el-form-item>
      <el-form-item label="3日" prop="day3">
        <el-input v-model="form.day3" placeholder="请输入3日" />
      </el-form-item>
      <el-form-item label="4日" prop="day4">
        <el-input v-model="form.day4" placeholder="请输入4日" />
      </el-form-item>
      <el-form-item label="5日" prop="day5">
        <el-input v-model="form.day5" placeholder="请输入5日" />
      </el-form-item>
      <el-form-item label="6日" prop="day6">
        <el-input v-model="form.day6" placeholder="请输入6日" />
      </el-form-item>
      <el-form-item label="7日" prop="day7">
        <el-input v-model="form.day7" placeholder="请输入7日" />
      </el-form-item>
      <el-form-item label="8日" prop="day8">
        <el-input v-model="form.day8" placeholder="请输入8日" />
      </el-form-item>
      <el-form-item label="9日" prop="day9">
        <el-input v-model="form.day9" placeholder="请输入9日" />
      </el-form-item>
      <el-form-item label="10日" prop="day10">
        <el-input v-model="form.day10" placeholder="请输入10日" />
      </el-form-item>
      <el-form-item label="11日" prop="day11">
        <el-input v-model="form.day11" placeholder="请输入11日" />
      </el-form-item>
      <el-form-item label="12日" prop="day12">
        <el-input v-model="form.day12" placeholder="请输入12日" />
      </el-form-item>
      <el-form-item label="13日" prop="day13">
        <el-input v-model="form.day13" placeholder="请输入13日" />
      </el-form-item>
      <el-form-item label="14日" prop="day14">
        <el-input v-model="form.day14" placeholder="请输入14日" />
      </el-form-item>
      <el-form-item label="15日" prop="day15">
        <el-input v-model="form.day15" placeholder="请输入15日" />
      </el-form-item>
      <el-form-item label="16日" prop="day16">
        <el-input v-model="form.day16" placeholder="请输入16日" />
      </el-form-item>
      <el-form-item label="17日" prop="day17">
        <el-input v-model="form.day17" placeholder="请输入17日" />
      </el-form-item>
      <el-form-item label="18日" prop="day18">
        <el-input v-model="form.day18" placeholder="请输入18日" />
      </el-form-item>
      <el-form-item label="19日" prop="day19">
        <el-input v-model="form.day19" placeholder="请输入19日" />
      </el-form-item>
      <el-form-item label="20日" prop="day20">
        <el-input v-model="form.day20" placeholder="请输入20日" />
      </el-form-item>
      <el-form-item label="21日" prop="day21">
        <el-input v-model="form.day21" placeholder="请输入21日" />
      </el-form-item>
      <el-form-item label="22日" prop="day22">
        <el-input v-model="form.day22" placeholder="请输入22日" />
      </el-form-item>
      <el-form-item label="23日" prop="day23">
        <el-input v-model="form.day23" placeholder="请输入23日" />
      </el-form-item>
      <el-form-item label="24日" prop="day24">
        <el-input v-model="form.day24" placeholder="请输入24日" />
      </el-form-item>
      <el-form-item label="25日" prop="day25">
        <el-input v-model="form.day25" placeholder="请输入25日" />
      </el-form-item>
      <el-form-item label="26日" prop="day26">
        <el-input v-model="form.day26" placeholder="请输入26日" />
      </el-form-item>
      <el-form-item label="27日" prop="day27">
        <el-input v-model="form.day27" placeholder="请输入27日" />
      </el-form-item>
      <el-form-item label="28日" prop="day28">
        <el-input v-model="form.day28" placeholder="请输入28日" />
      </el-form-item>
      <el-form-item label="29日" prop="day29">
        <el-input v-model="form.day29" placeholder="请输入29日" />
      </el-form-item>
      <el-form-item label="30日" prop="day30">
        <el-input v-model="form.day30" placeholder="请输入30日" />
      </el-form-item>
      <el-form-item label="31日" prop="day31">
        <el-input v-model="form.day31" placeholder="请输入31日" />
      </el-form-item>
      <el-form-item label="删除标志" prop="delFlag">
        <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Kh_monthly_attendance">
import { listKh_monthly_attendance, getKh_monthly_attendance, delKh_monthly_attendance, addKh_monthly_attendance, updateKh_monthly_attendance } from "@/api/kaoqin/kh_monthly_attendance";

const { proxy } = getCurrentInstance();

const kh_monthly_attendanceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const date_list = ref([]);
const years= ref(['2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030']);
const months= ref(['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']);

onMounted(() => {
  get_date_list()
});

// 生成类似2019-01数据date_list
const get_date_list = () => {
  for (let i = 0; i < years.value.length; i++) {
    for (let j = 0; j < months.value.length; j++)
      date_list.value.push(years.value[i] + '-' + months.value[j])
  }
}


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    userId: null,
    khMonth: null,
    amPm: null,
    khWork: null,
  },
  rules: {
    userName: [
      { required: true, message: "用户姓名不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "用户编号不能为空", trigger: "blur" }
    ],
    khMonth: [
      { required: true, message: "考勤月份不能为空", trigger: "change" }
    ],
    amPm: [
      { required: true, message: "考勤时段不能为空", trigger: "change" }
    ],
    khWork: [
      { required: true, message: "考勤类型不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询考勤月报表列表 */
function getList() {
  loading.value = true;
  listKh_monthly_attendance(queryParams.value).then(response => {
    kh_monthly_attendanceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    userName: null,
    userId: null,
    khMonth: null,
    amPm: null,
    khWork: null,
    day1: null,
    day2: null,
    day3: null,
    day4: null,
    day5: null,
    day6: null,
    day7: null,
    day8: null,
    day9: null,
    day10: null,
    day11: null,
    day12: null,
    day13: null,
    day14: null,
    day15: null,
    day16: null,
    day17: null,
    day18: null,
    day19: null,
    day20: null,
    day21: null,
    day22: null,
    day23: null,
    day24: null,
    day25: null,
    day26: null,
    day27: null,
    day28: null,
    day29: null,
    day30: null,
    day31: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("kh_monthly_attendanceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加考勤月报表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getKh_monthly_attendance(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改考勤月报表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["kh_monthly_attendanceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateKh_monthly_attendance(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addKh_monthly_attendance(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除考勤月报表编号为"' + _ids + '"的数据项？').then(function() {
    return delKh_monthly_attendance(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('kaoqin/kh_monthly_attendance/export', {
    ...queryParams.value
  }, `kh_monthly_attendance_${new Date().getTime()}.xlsx`);
}

getList();
</script>